﻿using MF_Base.Model;
using MFTool;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UIBase;
using MF_Base;
using System.Data;
         
namespace AutoUI.Areas.ConfigUIDef.Controllers
{
    [Export]
    public class FormCtrlDetailController : BaseController
    {
        //public ActionResult TextBox()
        //{
        //    return View();
        //}

        //public ActionResult ComboBox()
        //{
        //    return View();
        //}

        public ActionResult SubDataGrid()
        {
            var fieldTypeList = SqlHelper.SQlCommonServerTypeList().Select(a => new { text = a, value = a });
            ViewBag.FieldType = fieldTypeList.ToJson();

            return View();
        }

        public JsonResult GetEnumList()
        {
            var res = UnitOfWork.Get<MF_Enum>().Select(a => new { Code = a.Code, Name = a.Name }).Distinct();
            return Json(res);
        }

        public JsonResult GetSubDataGrid(string formId, string fieldName)
        {
            formId.CheckNotNullOrEmpty("formId");
            FormConfig cForm = UnitOfWork.GetByKey<FormConfig>(formId);
            cForm.CheckNotNull("FormConfig");
            var resList = cForm.GetSubTablePropertyDicList(fieldName);

            //默认值
            foreach(var res in resList)
            {
                res.SetValue("IsVisible", "是");
                res.SetValue("Enable", "是");
                res.SetValue("ColumnName", res.GetValue("FieldName"));
            }
            return Json(resList);
        }

        public JsonResult GetTableFieldNames(string tableName, string connName)
        {
            string sql = "Select Name FROM SysColumns Where id=Object_Id('{0}')".ReplaceArg(tableName);
            var resList = SqlHelper.CreateSqlHelper(connName).ExcuteTable(sql).AsEnumerable();
            return Json(resList.Select(a => new { text = a["Name"].ToString(), value = a["Name"].ToString() }));
        }

        public JsonResult LoadFuncList()
        {
            string id = QueryString("formConfigId");
            FormConfig fc = UnitOfWork.GetByKey<FormConfig>(id);
            fc.CheckNotNull("FormConfig");
            var tmp = fc.GetScriptFunctionList().Select(a => new { text = a, value = a });
            return Json(fc.GetScriptFunctionList().Select(a => new { text = a, value = a }));
        }
    }
}